#!/bin/echo Warning: this library should be sourced!

cd `dirname $0`
CUR_DIR_TMP=`pwd`
TOOLS=$CUR_DIR_TMP/../lib

function log_info()
{
    echo "[`date +%Y-%m-%d\ %T`] [  INFO ] $@"
}

function log_warn()
{
    echo -e "\033[33m"[`date +%Y-%m-%d\ %T`] [WARNING] $@" \033[0m"
}

function log_error()
{
    echo -e "\033[31m"[`date +%Y-%m-%d\ %T`] [ ERROR ] $@" \033[0m"
    exit 1
}


function log_debug()
{
    [ "$DEBUG" == "yes" ] && echo "[`date +%Y-%m-%d\ %T`] [ DEBUG ] $@"
    echo -n ""
}

function clean_and_exit()
{
    
    if [ $1 -ne 0 ]; then
        echo "=========error start========="
        cat $ERROR_LOG
        echo "=========error end========="
    fi
    exit $1
}

function run_srcipt()
{
    script=$1
    shift
    args="$@"
    log_info "Start run $script $args at `date`"
    bash $script $args
    if [ $? -ne 0 ]; then
        log_error "Run $script $args failed at `date`"
    fi
    log_info "Finished run $script $args at `date`"
}

function sshcmd()
{
    set +e
    MYSSH_CMD="$1"
    sleeptime=0
    sh "$TOOLS"/sshcmd.sh -c "$MYSSH_CMD" -m "$IP" -u "$USER" -p "$PASSWD"
    if [ $? -ne 0 ]; then
        while true
        do
            if ping -c5 "$IP" &> /dev/null; then
                break
            fi
            if [ $sleeptime -ge 180 ]; then
                echo "ERROR: ping $IP Failed"
                break
            fi
            sleep 60
            ((sleeptime = sleeptime + 60))
        done
        sh "$TOOLS"/sshcmd.sh -c "$MYSSH_CMD" -m "$IP" -u "$USER" -p "$PASSWD"
        if [ $? -ne 0 ]; then
            sleep 10
            sh "$TOOLS"/sshcmd.sh -c "$MYSSH_CMD" -m "$IP" -u "$USER" -p "$PASSWD"
            if [ $? -ne 0 ]; then
                echo "Failed in sshcmd.sh on $MYSSH_CMD, maybe there is no enough space on $IP"
                return 1
            fi
        fi
    fi
}

function sshcmd_once()
{
    set +e
    MYSSH_CMD="$1"
    sleeptime=0
    sh "$TOOLS"/sshcmd.sh -c "$MYSSH_CMD" -m "$IP" -u "$USER" -p "$PASSWD"
    return $?
}

function sshscp()
{
    MYSOURCE="$1"
    MYDESTDIR="$2"
    sleeptime=0
    sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$MYSOURCE" -d "$IP:$MYDESTDIR" -r
    if [ $? -ne 0 ]; then
        while true
        do
            if ping -c5 "$IP" &> /dev/null; then
                break
            fi
            if [ $sleeptime -ge 180 ]; then
                echo "ERROR: ping $IP Failed"
                break
            fi
            sleep 60
            ((sleeptime = sleeptime + 60))
        done
        sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$MYSOURCE" -d "$IP:$MYDESTDIR"
        if [ $? -ne 0 ]; then
            sleep 10
            sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$MYSOURCE" -d "$IP:$MYDESTDIR"
            if [ $? -ne 0 ]; then
                echo "Failed in sshcmd.sh on $MYSSH_CMD, maybe there is no enough space on $IP"
                exit 1
            fi
            echo "Failed in sshscp.sh on $MYSOURCE, maybe there is no enough space on $IP"
            exit 1
        fi
    fi
}

function sshscp_from()
{
    MYSOURCE="$1"
    MYDESTDIR="$2"
    sleeptime=0
    sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$IP:$MYSOURCE" -d "$MYDESTDIR" -r
    if [ $? -ne 0 ]; then
        while true
        do
            if ping -c5 "$IP" &> /dev/null; then
                break
            fi
            if [ $sleeptime -ge 180 ]; then
                echo "ERROR: ping $IP Failed"
                break
            fi
            sleep 60
            ((sleeptime = sleeptime + 60))
        done
        sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$IP:$MYSOURCE" -d "$MYDESTDIR"
        if [ $? -ne 0 ]; then
            sleep 10
            sh "$TOOLS"/sshscp.sh -p "$PASSWD" -s "$IP:$MYSOURCE" -d "$MYDESTDIR"
            if [ $? -ne 0 ]; then
                echo "Failed in sshcmd.sh on $MYSSH_CMD, maybe there is no enough space on $IP"
                exit 1
            fi
            echo "Failed in sshscp.sh on $MYSOURCE, maybe there is no enough space on $IP"
            exit 1
        fi
    fi
}